import numpy as np
from sklearn import tree
from graphviz import Source

np.random.seed(42)                                  # 随机种子
X = np.random.randint(10, size=(100, 4))            # 0-10，100行4列
Y = np.random.randint(2, size=100)                  # 0-2，100个
a = np.column_stack((Y, X))                         # X的第n行最后面加上Y中的第n个元素；a没用到

clf = tree.DecisionTreeClassifier(criterion='gini', max_depth=3)  #用基尼系数、深度为3来初始化决策树分类器
clf = clf.fit(X, Y)                                 # 拟合，传入X，Y

graph = Source(tree.export_graphviz(clf, out_file=None))
graph.format = 'png'
graph.render('cart_tree', view=True)


